package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bizlogic.BookingManager;
import bizlogic.FacilityManager;

import model.BookingDTO;
import model.FacilityDTO;
import model.search.BookingSearchDTO;
import model.search.FacilitySearchDTO;

import util.UrlUtility;

@WebServlet("/SearchEmptyFacility")
public class SearchEmptyFacilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SearchEmptyFacilityServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doShow(request, response);
	}
	
	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		UrlUtility.forwardPage(req, res, "/WEB-INF/SearchEmptyFacility.jsp");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	
	private BookingSearchDTO prepareSearch(String facility, Date start, Date end, ArrayList<String> status) {
		BookingSearchDTO bs = new BookingSearchDTO();
		bs.setFacilityName(facility);
		bs.setStartDateFrom(start);
		bs.setStartDateTo(end);
		bs.setEndDateFrom(start);
		bs.setEndDateTo(end);
		bs.setStatus(status);
		
		return bs;
	}
	
	private void doPrepareMap(HttpServletRequest req, HttpServletResponse res, Date start, Date end) {
		SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
		ArrayList<String> dateList = new ArrayList<String>();
		ArrayList<Boolean> dateSchedule = new ArrayList<Boolean>();

		Calendar from = Calendar.getInstance();
		from.setTime(start);
		Calendar to = Calendar.getInstance();
		to.setTime(end);
		
		// Preparing the date list
		for (; !from.after(to); from.add(Calendar.DATE, 1)) {
			Date current = from.getTime();
			
			dateList.add(df.format(current));
			//dateSchedule.add(new Boolean(true));
		}
		
		// Get the Facility List
		FacilitySearchDTO src = new FacilitySearchDTO();
		src.setType((String) req.getAttribute("type"));
		src.setStatus(1);

		FacilityManager fm = new FacilityManager();
		ArrayList<FacilityDTO> list = fm.findFacilityByCriteria(src);

		/*BookingSearchDTO bs = new BookingSearchDTO();
		bs.setStartDateFrom(start);
		bs.setStartDateTo(end);
		bs.setEndDateFrom(start);
		bs.setEndDateTo(end);
		bs.setStatus("Approve");*/

		BookingManager bm = new BookingManager();
		//ArrayList<BookingDTO> bookingList = new ArrayList<BookingDTO>();
		
		HashMap<String, ArrayList<Boolean>> hm = new HashMap<String, ArrayList<Boolean>>();
		for ( FacilityDTO f : list ) {
			/*bs.setFacilityName(f.getName());*/
			ArrayList<String> status = new ArrayList<String>();
			status.add("Approve");
			BookingSearchDTO bs = prepareSearch(f.getName(), start, end, status);
			
			dateSchedule = new ArrayList<Boolean>();
			from.setTime(start);
			to.setTime(end);
			for (; !from.after(to); from.add(Calendar.DATE, 1)) {
				dateSchedule.add(new Boolean(true));
			}
			
			//ArrayList<BookingDTO> bookingList = new ArrayList<BookingDTO>();
			ArrayList<BookingDTO> bookingList = bm.findBookingByCriteria(bs);
			
			
			for ( BookingDTO b : bookingList ) {
				int i = 0;
				from.setTime(start);
				to.setTime(end);
				for (; !from.after(to); from.add(Calendar.DATE, 1)) {
					if ( from.getTime().before(b.getStartDate()) ||
							from.getTime().after(b.getEndDate()) ) {
/*						if ( i == dateSchedule.size() ) {
							dateSchedule.add(new Boolean(true));
						}*/ 
					} else {
						if ( i < dateSchedule.size() ) {
							dateSchedule.set(i, new Boolean(false));
						} /*else {
							dateSchedule.add(new Boolean(false));
						}*/
					}
					i++;
				}
			}
			
			hm.put(f.getName(), dateSchedule);
		}
		
		// Set the availability
		
		req.setAttribute("dateList", dateList);
		req.setAttribute("schedule", hm);
	}
	
	private void doProcess(HttpServletRequest req, HttpServletResponse res) {
		String address = "/WEB-INF/SearchEmptyFacility.jsp";
		SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
		
		String type = req.getParameter("type");
		Date startDate = null, endDate = null;
		
		try {
			startDate = df.parse(req.getParameter("startDate"));
			endDate = df.parse(req.getParameter("endDate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		req.setAttribute("type", type);
		req.setAttribute("startDate", df.format(startDate));
		req.setAttribute("endDate", df.format(endDate));
		
		if ( type == null ) {
			
		} else if ( startDate == null ) {
			
		} else if ( endDate == null ) {
			
		} else if ( startDate.after(endDate) ) {
			
		} else { // Prepare the data
			System.out.println("Do Prepare Map:");
			doPrepareMap(req, res, startDate, endDate);
		}
		
		UrlUtility.forwardPage(req, res, address);
	}

}
